/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.google.api.client.util.IOUtils;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import lombok.SneakyThrows;
import org.springblade.common.cache.UserCache;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.oss.model.OssFile;
import org.springblade.core.oss.props.OssProperties;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.mach.entity.*;
import org.springblade.mach.service.*;
import org.springblade.mach.utils.ExcelUtils;
import org.springblade.mach.ziputils.ZipUtil;
import org.springblade.modules.resource.builder.oss.MinioOssBuilder;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.service.IAttachService;
import org.springblade.modules.resource.vo.AttachVO;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.mach.vo.DrawingInfoVO;
import org.springblade.mach.wrapper.DrawingInfoWrapper;
import org.springblade.core.boot.ctrl.BladeController;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

/**
 *  控制器
 *
 * @author LY
 * @since 2021-03-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("data-drawing-info/drawinginfo")
@Api(value = "", tags = "接口")
public class DrawingInfoController extends BladeController {

	private final IDrawingInfoService drawingInfoService;
	// 附件上传upload
	private final IDrawingUploadService drawingUploadService;
	// 附件信息
	private final IAttachService attachService;
	// 对象存储对象
	private OssBuilder ossBuilder;
	// 对象存储的配置文件
	private OssProperties ossProperties;
	// 操作excel的对象
	private ExcelUtils excelUtils;
	// 保存父子节点信息
	private IDrawingInfoNodeService drawingInfoNodeService;
	// 图纸类型
	private IDrawingCategoryService drawingCategoryService;
	// 标准物料
	private IStandardPartsService standardPartsService;
	// 采购件表
	private IPurchasedPartsService purchasedPartsService;
	// info-attach对应表
	private IDrawingInfoAttachService drawingInfoAttachService;
	// info-project
	private IDrawingInfoProjectService drawingInfoProjectService;
	private IDrawingNumberApplicationDetailService drawingNumberApplicationDetailService;
	// ziputils
	private ZipUtil zipUtil;
	/**
	 * 修改图面信息的 展示节点关联的所有的父节点 以及子节点的信息
	 *
	 */
	@GetMapping("/getAllNodeByInfoId")
	public R<Map<String,Object>> getAllData(@RequestParam Long infoId){
		DrawingInfo drawingInfo = drawingInfoService.getById(infoId);
		Map<String,Object> map = new HashMap<>();

		// 获取所有的上级节点
		List<DrawingInfo> parentDrawingInfoList = drawingInfoService.findParentDrawingInfoList(drawingInfo.getDrawingNumber());
		map.put("super",parentDrawingInfoList);

		// 获取所有的子级节点
		List<DrawingInfo> childDrawingInfoList = drawingInfoService.findChildDrawingInfoList(infoId);
		map.put("sub",childDrawingInfoList);

		// 合并元素
//		parentDrawingInfoList.addAll(childDrawingInfoList);
		return R.data(map);

	}


	/**
	 * 图纸下载 单张下载  单级 整套
	 */
	@GetMapping("/getSoloDrawing")
	public void getSoloDrawing(@RequestParam Long infoId, @RequestParam Integer downType,HttpServletResponse response) throws IOException {
		if (downType!=null) {
			List<Attach> solo = attachService.getAttachByInfoId(infoId);
			if (solo.size() > 0) {
				response.setHeader("Content-Disposition", "attachment;filename="+
					URLEncoder.encode(solo.get(0).getOriginalName().substring(0,solo.get(0).getOriginalName().lastIndexOf(".")), "UTF-8")+".zip");
			}
			switch (downType) {
				case 1:
					if (!io.jsonwebtoken.lang.Collections.isEmpty(solo)) {
						zipUtil.toZip(solo, response.getOutputStream());
					}
					break;
				case 2:
					// infoid下所有的子节点
					List<Long> allChild = drawingInfoService.getChileNodeByPId(infoId).stream()
						.filter(vo -> vo.getDrawingNumber().contains("MH"))
						.map(DrawingInfoVO::getId)
						.collect(Collectors.toList());
					allChild.add(infoId);
					List<Attach> singleAttach = attachService.getSingleAttachByInfoId(allChild);
					if (!io.jsonwebtoken.lang.Collections.isEmpty(singleAttach)) {
						zipUtil.toZip(singleAttach, response.getOutputStream());
					}
					break;
				case 3:
					// infoid节点展开下 所有的节点的信息
					List<DrawingInfo> childDrawingInfoList = drawingInfoService.findChildDrawingInfoList(infoId);
					List<Long> allLevelChild = childDrawingInfoList.stream().map(DrawingInfo::getId).collect(Collectors.toList());
					allLevelChild.add(infoId);
					List<Attach> all = attachService.getSingleAttachByInfoId(allLevelChild);
					if (!io.jsonwebtoken.lang.Collections.isEmpty(all)) {
						zipUtil.toZip(all, response.getOutputStream());
					}
					break;
			}
		}
	}



	/**
	 * 展示当前根节点下，所有的子节点的记录
	 */
	@GetMapping("/getChildNodeByPId")
	public R<Map<String,Object>> getChildListByPid(Long pid){
		Map<String,Object> map = new HashMap<>();

		List<DrawingInfoVO> childNodeList = drawingInfoService.getChileNodeByPId(pid);

		childNodeList.stream().forEach(drawingInfoVO -> {
			String drawingType = drawingInfoVO.getDrawingType();
			if (drawingInfoVO.getDrawingNumber().contains("MH")) {
				// 图纸类型展示字段
				if (StringUtil.isNotBlank(drawingType)) {
					DrawingCategory code = drawingCategoryService.getOne(new QueryWrapper<DrawingCategory>().eq("code", drawingType));
					if (code!=null) {
						drawingInfoVO.setDrawingTypeName(code.getName());
					}
				}
				// 获取审批完的附件对应的附件地址信息
				List<Attach> fileList = drawingInfoAttachService.getAttachListByDrawingInfoId(drawingInfoVO.getId());
				if (fileList.size()>0) {
					drawingInfoVO.setFileAddress(fileList);
				}
			}
		});
		List<DrawingInfoVO> eqpList = childNodeList.stream().filter(u -> u.getDrawingNumber().contains("MH")).collect(Collectors.toList());

		List<DrawingInfoVO> materialList = childNodeList.stream().filter(u -> !(u.getDrawingNumber().contains("MH"))).collect(Collectors.toList());

        if (eqpList.size()>0) {
        	map.put("eqpList",eqpList);
		}
        if (materialList.size()>0) {
			map.put("materialList",materialList);
		}
		return R.data(map);

	}

	/**
	 * 展示当前根节点下，所有的附件信息
	 */
	@GetMapping("/getAttachListByPid")
	public R<List<Attach>> getAttachList(Long pid){

		return R.data(attachService.getAttachByInfoId(pid));
	}

	/**
	 * 展示当前根节点下 所有的图片信息
	 */
	@GetMapping("/getImgAttachListByPid")
	public R<List<Attach>> getImgList(Long pid){
		List<Attach> attachByInfoId = attachService.getAttachByInfoId(pid);
		List<Attach> imgList = attachByInfoId.stream().filter(u -> u.getType().contains("图片")).collect(Collectors.toList());
		return R.data(imgList);
	}


	/**
	 * 右侧查询数据的展示
	 * @param drawingInfo
	 * @param query
	 * @return
	 */
	@GetMapping("/getRightList")
	public R<IPage<DrawingInfoVO>> getRightList(String drawingInfo, Query query){
		DrawingInfo drawingInfoSearch = Func.readJson(drawingInfo, DrawingInfo.class);
		IPage<DrawingInfoVO> drawingInfoVOIPage = drawingInfoService.selectDrawingInfoPage(Condition.getPage(query), drawingInfoSearch);
        drawingInfoVOIPage.getRecords().stream().forEach(drawingInfoVO -> {
			String drawingType = drawingInfoVO.getDrawingType();
			if (drawingInfoVO.getDrawingNumber().contains("MH") && StringUtil.isNotBlank(drawingType)) {
				DrawingCategory code = drawingCategoryService.getOne(new QueryWrapper<DrawingCategory>().eq("code", drawingType));
				if (code!=null) {
					drawingInfoVO.setDrawingTypeName(code.getName());
				}
			}

		});
        return R.data(drawingInfoVOIPage);
	}

	/**
	 * 左侧树结构的展示
	 * @param drawingInfo
	 * @return
	 */
	@GetMapping("/getTreeList")
	public R<List<DrawingInfoVO>> getTreeList(String drawingInfo){
		DrawingInfo drawingInfoSearch = Func.readJson(drawingInfo, DrawingInfo.class);
		return R.data(drawingInfoService.getTreeList(drawingInfoSearch));
	}


	/**
	 * 懒加载树形结构
	 */
	@GetMapping("/lazy-tree")
	public R<List<DrawingInfoVO>> getLazyTreeNode(Long parentId, Long id){
         return R.data(drawingInfoService.lazyTree(parentId,id));
	}



	/**
	 * 获取后台所有的树节点信息
	 */
	@GetMapping("/getTreeNodeList")
	public R<List<DrawingInfoVO>> getTreeNode(){
		return  R.data(drawingInfoService.tree());
	}

	/**
	 * 查询 状态为3 归档状态的图纸信息
	 * 对于已经归档的excel:
	 * (1) 拿到excel文件
	 * (2) 形成BOM树形结构
	 * (3) 按照属性 区分标准件、采购件信息列表
	 */
	@GetMapping("/getFileList")
	@SneakyThrows(Exception.class)
	public void getDrawingBom(Long id){
		// 获取uploadId所有的附件信息
		List<AttachVO> allAttachList = attachService.findAllUploadId(id);

		// List元素按照附件名称进行分类
		Map<String,List<AttachVO>> collect = allAttachList.stream().collect(Collectors.groupingBy(AttachVO::getOriginalName));

		// step1:先整合excel中对应的bom信息
		List<AttachVO> allExcel = attachService.getBomExcelByUploadId(id);    // 这个是所有的excel的附件信息

		for (int i = 0; i <allExcel.size() ; i++) {
			// 设备编号
			String drawingNumber = allExcel.get(i).getOriginalName().substring(0,13);
			if (!drawingNumber.substring(5,7).equals("00")) {
				String fileNameServer = allExcel.get(i).getName();
				// 父节点信息
				DrawingInfo parent = drawingInfoService.getDrawingInfoByDrawingNumber(drawingNumber);
				if (parent == null) {
					parent = new DrawingInfo();
					parent.setDrawingNumber(drawingNumber);
					if (drawingNumber.contains("MH")) {
						// 图纸类型
						parent.setDrawingType(drawingNumber.substring(5,7));
					}
					drawingInfoService.save(parent);
				}
				// 如果节点信息存在 不需要重复传入数据
				DrawingInfoNode parentNode = drawingInfoNodeService.getOne(new QueryWrapper<DrawingInfoNode>().
					eq("parent_id",0).
					eq("child_id",parent.getId()));
				if (parentNode == null) {
					parentNode = new DrawingInfoNode();
					// 当前父节点信息
					parentNode.setParentId(0L);
					parentNode.setChildId(parent.getId());
					parentNode.setNum("1");
					drawingInfoNodeService.save(parentNode);
				}

				// 父节点对应的attach_id 判断记录是否存在
				if (drawingNumber.contains("MH")) {
					// 模糊匹配Map中的值
					for (Map.Entry entity : collect.entrySet()) {
						// 表示记录存在 如果没有找到记录，返回-1
						if (((String) entity.getKey()).substring(0,13).indexOf(drawingNumber) > -1) {
							List<AttachVO> attachList = collect.get(entity.getKey());
							for (int j = 0; j <attachList.size() ; j++) {
								DrawingInfoAttach infoAttach = drawingInfoAttachService.getOne(new QueryWrapper<DrawingInfoAttach>().
									eq("drawing_info_id",parent.getId()).
									eq("attach_id",attachList.get(j).getId()).
									eq("version","1"));
								if (infoAttach == null) {
									infoAttach = new DrawingInfoAttach();
									// 父节点对应的attach_id
									infoAttach.setAttachId(attachList.get(j).getId());
									infoAttach.setDrawingInfoId(parent.getId());
									infoAttach.setVersion("1");
									drawingInfoAttachService.save(infoAttach);
								}
							}
						}
					}
				}

				// 读取内容
				List<ReadExcelEntity> excelEntityList = excelUtils.getExcelEntityList("000000-machtech", fileNameServer);

				for (int j = 0; j <excelEntityList.size() ; j++) {
					// ##########step2处理info表中的记录
					// 读取的bom信息
					ReadExcelEntity readExcelEntity = excelEntityList.get(j);
					// BOM的info明细
					DrawingInfo drawingInfoDB = null;
					if (readExcelEntity.getDrawingNumber().contains("MH")) {
						drawingInfoDB = drawingInfoService.getOne(new QueryWrapper<DrawingInfo>().
							eq("drawing_number",readExcelEntity.getDrawingNumber()));
					}else {
						// 非MH的BOM明细
						drawingInfoDB = drawingInfoService.getOne(new QueryWrapper<DrawingInfo>().
							eq("drawing_number",readExcelEntity.getDrawingNumber()).
							eq("drawing_name",readExcelEntity.getDrawingName()).
							eq("remark", readExcelEntity.getRemark()));
					}

					// 处理info表中bom信息  更新操作
					if (drawingInfoDB != null) {
						drawingInfoDB.setDrawingNumber(readExcelEntity.getDrawingNumber());
						drawingInfoDB.setDrawingName(readExcelEntity.getDrawingName());
						drawingInfoDB.setMaterial(readExcelEntity.getMaterial());
						drawingInfoDB.setSingleWeight(readExcelEntity.getSingleWeight());
						drawingInfoDB.setDrawingProperty(readExcelEntity.getDrawingProperty());
						drawingInfoDB.setRemark(readExcelEntity.getRemark());
						drawingInfoDB.setCharacOne(readExcelEntity.getCharacOne());
						drawingInfoDB.setCharacTwo(readExcelEntity.getCharacTwo());
						drawingInfoDB.setCharacThree(readExcelEntity.getCharacThree());
						if (readExcelEntity.getDrawingNumber().contains("MH")) {
							drawingInfoDB.setDrawingType(readExcelEntity.getDrawingNumber().substring(5,7));
							// 保存attach_id表中的记录
						}
						drawingInfoService.updateById(drawingInfoDB);

					}else{
						if (readExcelEntity.getDrawingNumber().contains("MH")) {
							readExcelEntity.setDrawingType(readExcelEntity.getDrawingNumber().substring(5,7));
						}
						boolean save = drawingInfoService.save(readExcelEntity);
						if (save) {
							if (readExcelEntity.getDrawingNumber().contains("MH")) {
								// 图纸编号  节点信息展示，info-attach
								String number = readExcelEntity.getDrawingNumber();
								for (Map.Entry entity :  collect.entrySet()) {
									if (((String) entity.getKey()).substring(0,13).indexOf(number) > -1) {
										List<AttachVO> attachList = collect.get(entity.getKey());
										for (int k = 0; k <attachList.size() ; k++) {
											DrawingInfoAttach infoAttach = drawingInfoAttachService.getOne(new QueryWrapper<DrawingInfoAttach>().
												eq("drawing_info_id",readExcelEntity.getId()).
												eq("attach_id",attachList.get(k).getId()).
												eq("version","1"));
											if (infoAttach == null) {
												infoAttach = new DrawingInfoAttach();
												// 父节点对应的attach_id
												infoAttach.setAttachId(attachList.get(k).getId());
												infoAttach.setDrawingInfoId(readExcelEntity.getId());
												infoAttach.setVersion("1");
												drawingInfoAttachService.save(infoAttach);
											}
										}
									}
								}
							}
						}
					}

					// 数据库中的记录
					DrawingInfo info = drawingInfoService.getOne(new QueryWrapper<DrawingInfo>().
						eq("drawing_number",readExcelEntity.getDrawingNumber()).
						eq("drawing_name",readExcelEntity.getDrawingName()));

					// node表中节点信息
					DrawingInfoNode node  = drawingInfoNodeService.getOne(new QueryWrapper<DrawingInfoNode>().
						eq("parent_id",parent.getId()).
						eq("child_id",info.getId()));
					if (node == null) {
						node = new DrawingInfoNode();
						node.setParentId(parent.getId());
						node.setChildId(info.getId());
						node.setNum(readExcelEntity.getBomNum());
						drawingInfoNodeService.save(node);
					}

					//###########step3:处理物料标准库表中的记录(现统计为属性值 应该为备注信息)
					if (StringUtil.equals(readExcelEntity.getDrawingProperty(),"标准件")) {
						// 查询数据库中是否有记录
						StandardParts standardPartsDB =  standardPartsService.getOne(new QueryWrapper<StandardParts>().
							eq("code",readExcelEntity.getDrawingNumber()).
							eq("name",readExcelEntity.getDrawingName()));
						if (standardPartsDB!=null) {
							standardPartsDB.setCode(readExcelEntity.getDrawingNumber());
							standardPartsDB.setName(readExcelEntity.getDrawingName());
							standardPartsDB.setMaterial(readExcelEntity.getMaterial());
							standardPartsDB.setSingleWeight(readExcelEntity.getSingleWeight());
							standardPartsService.updateById(standardPartsDB);
						}else{
							// 无记录时，进行新增记录
							StandardParts standardParts = new StandardParts();
							standardParts.setCode(readExcelEntity.getDrawingNumber());
							standardParts.setName(readExcelEntity.getDrawingName());
							standardParts.setMaterial(readExcelEntity.getMaterial());
							standardParts.setSingleWeight(readExcelEntity.getSingleWeight());
							standardPartsService.save(standardParts);
						}
					}

					//###########step4:处理采购件表中的记录(现统计为属性值 应该为备注信息)
					if (StringUtil.equals(readExcelEntity.getDrawingProperty(),"采购件")) {
						// 查询数据库中是否有记录
						PurchasedParts purchasedPartsDB =  purchasedPartsService.getOne(new QueryWrapper<PurchasedParts>().
							eq("code",readExcelEntity.getDrawingNumber()).
							eq("name",readExcelEntity.getDrawingName()));
						if (purchasedPartsDB!=null) {
							purchasedPartsDB.setCode(readExcelEntity.getDrawingNumber());
							purchasedPartsDB.setName(readExcelEntity.getDrawingName());
							purchasedPartsDB.setMaterial(readExcelEntity.getMaterial());
							purchasedPartsDB.setSingleWeight(readExcelEntity.getSingleWeight());
							purchasedPartsService.updateById(purchasedPartsDB);
						}else{
							// 无记录时，进行新增记录
							PurchasedParts purchasedParts = new PurchasedParts();
							purchasedParts.setCode(readExcelEntity.getDrawingNumber());
							purchasedParts.setName(readExcelEntity.getDrawingName());
							purchasedParts.setMaterial(readExcelEntity.getMaterial());
							purchasedParts.setSingleWeight(readExcelEntity.getSingleWeight());
							purchasedPartsService.save(purchasedParts);
						}
					}
				}
			}
		}

		// 零件图对应的节点信息
		for (int i = 0; i <allExcel.size() ; i++) {
			String drawingNumber = allExcel.get(i).getOriginalName().substring(0,13);
			if (drawingNumber.substring(5,7).equals("00")) {
				if (drawingNumber.contains("MH")) {
					DrawingInfo drawingInfo = drawingInfoService.getDrawingInfoByDrawingNumber(drawingNumber);
					if (drawingInfo!=null) {
						// 模糊匹配Map中的值
						for (Map.Entry entity : collect.entrySet()) {
							// 表示记录存在 如果没有找到记录，返回-1
							if (((String) entity.getKey()).substring(0,13).indexOf(drawingNumber) > -1) {
								List<AttachVO> attachList = collect.get(entity.getKey());
								for (int j = 0; j <attachList.size() ; j++) {
									DrawingInfoAttach infoAttach = drawingInfoAttachService.getOne(new QueryWrapper<DrawingInfoAttach>().
										eq("drawing_info_id",drawingInfo.getId()).
										eq("attach_id",attachList.get(j).getId()).
										eq("version","1"));
									if (infoAttach == null) {
										infoAttach = new DrawingInfoAttach();
										// 父节点对应的attach_id
										infoAttach.setAttachId(attachList.get(j).getId());
										infoAttach.setDrawingInfoId(drawingInfo.getId());
										infoAttach.setVersion("1");
										drawingInfoAttachService.save(infoAttach);
									}
								}
							}
						}
					}
				}
			}
		}
	}




	public List<ReadExcelEntity> getRreadExcel(@RequestParam Long id) throws IOException, XmlPullParserException, NoSuchAlgorithmException, InvalidKeyException, InvalidPortException, InvalidArgumentException, ErrorResponseException, NoResponseException, InvalidBucketNameException, InsufficientDataException, InvalidEndpointException, InternalException {
		String fileNameServer = "upload/20210604/efe360abea66c13a41bb6eff52b953a3/MH61.01.00008-称量输送机.xls";
		List<ReadExcelEntity> excelEntityList = excelUtils.getExcelEntityList("000000-machtech", fileNameServer);
		return excelEntityList;
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入drawingInfo")
	public R<DrawingInfoVO> detail(DrawingInfo drawingInfo) {
		DrawingInfo detail = drawingInfoService.getOne(Condition.getQueryWrapper(drawingInfo));
		return R.data(DrawingInfoWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/lazy-list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入drawingInfo")
	public R<List<DrawingInfoVO>> list(Long parentId, @RequestParam Map<String,Object>  drawingInfo) {
		List<DrawingInfoVO> drawingInfoList =  drawingInfoService.lazyList(parentId,drawingInfo);
		drawingInfoList.stream().forEach(drawingInfoVO -> {
			drawingInfoVO.setUserAccount(UserCache.getUser(drawingInfoVO.getCreateUser()).getAccount());
		});

		return R.data(DrawingInfoWrapper.build().listNodeLazyVO(drawingInfoList));
	}

	@GetMapping("/tree")
	public R<List<DrawingInfoVO>> tree() {
		List<DrawingInfoVO> tree = drawingInfoService.tree();
		return R.data(tree);
	}

	@GetMapping("/getDrawingInfoByNodeId")
	public R<DrawingInfoVO> getDrawingInfoByNodeId(@RequestParam Long nodeId){
		DrawingInfo info = drawingInfoService.getOne(new QueryWrapper<DrawingInfo>().eq("id",nodeId));
		DrawingInfoVO drawingInfoVO = Objects.requireNonNull(BeanUtil.copy(info, DrawingInfoVO.class));
		String drawingType = info.getDrawingType();
		if (info.getDrawingNumber().contains("MH") && StringUtil.isNotBlank(drawingType)) {
			DrawingCategory code = drawingCategoryService.getOne(new QueryWrapper<DrawingCategory>().
				eq("code", drawingType));
			if (code!=null){
				drawingInfoVO.setDrawingTypeName(code.getName());
			}
		}
		return R.data(drawingInfoVO);
	}



	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入drawingInfo")
	public R<IPage<DrawingInfoVO>> page(DrawingInfoVO drawingInfo, Query query) {
		IPage<DrawingInfoVO> pages = drawingInfoService.selectDrawingInfoPage(Condition.getPage(query), drawingInfo);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入drawingInfo")
	public R save(@Valid @RequestBody DrawingInfo drawingInfo) {
		return R.status(drawingInfoService.save(drawingInfo));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入drawingInfo")
	public R update(@Valid @RequestBody DrawingInfo drawingInfo) {
		return R.status(drawingInfoService.updateById(drawingInfo));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入drawingInfo")
	public R submit(@Valid @RequestBody DrawingInfo drawingInfo) {
		return R.status(drawingInfoService.saveOrUpdate(drawingInfo));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(drawingInfoService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 获取父层级的图纸信息列表
	 * @param subDrawingNumber
	 * @return
	 */
	@GetMapping("/findParentDrawingInfoList")
	public R findParentDrawingInfoList(String subDrawingNumber) {
		List<DrawingInfo> drawingInfoList = drawingInfoService.findParentDrawingInfoList(subDrawingNumber);
		return R.data(drawingInfoList);
	}

	/**
	 * 获取所有的info中MH开头的节点的信息
	 */
	@GetMapping("/getMHInfoList")
	public R<List<DrawingInfo>> getMHList(){
		return R.data(drawingInfoService.list(new QueryWrapper<DrawingInfo>()
			.lambda()
			.like(DrawingInfo::getDrawingNumber, "MH")));
	}

	/**
	 * @Author zhp
	 * @Description //TODO
	 * @Date 16:59 2022/1/18
	 * @param drawingInfo
	 * @return org.springblade.core.tool.api.R
	 * 下线图纸,目前只是修改状态
	*/
	@RequestMapping("/offlineDrawingInfo")
	public R offlineDrawingInfo(@Valid @RequestBody DrawingInfo drawingInfo){
		drawingInfo.setStatus(DrawingInfo.STATUS_OFFLINE);
		//查询出对应图号详情信息
		final DrawingNumberApplicationDetail drawingNumberApplicationDetail = drawingNumberApplicationDetailService.getOne(
			new QueryWrapper<DrawingNumberApplicationDetail>()
				.eq("number", drawingInfo.getDrawingNumber()));
		//将对应图号状态改为已下线
		drawingNumberApplicationDetail.setStatus(DrawingNumberApplicationDetail.STATUS_OFFLINE);
		drawingNumberApplicationDetailService.updateById(drawingNumberApplicationDetail);
		return R.status(drawingInfoService.updateById(drawingInfo));
	}
}
